package studysystem.demo.controller;

import com.github.pagehelper.PageHelper;
import org.apache.shiro.codec.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import studysystem.demo.bean.Learner;
import studysystem.demo.bean.Course;
import studysystem.demo.mapper.CourseMapper;
import studysystem.demo.mapper.LearnerMapper;
import studysystem.demo.tool.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;
@Controller
public class CourseController {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private LearnerMapper learnerMapper;

    //管理员添加视频资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/video",method = RequestMethod.POST)
    @ResponseBody
    public Mess addVideo(@RequestBody Course courseEntity){
        String b;
        if(courseMapper.checkVideo_url(courseEntity.getVideo_url())==null)
        {
        courseMapper.addVedio(courseEntity);
         b="视频添加成功!";
        }
        else b="该视频已存在！";
        return Mess.info(b);
    }

    //管理员添加文档资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/doc",method = RequestMethod.POST)
    @ResponseBody
    public Mess addDoc(@RequestBody Course courseEntity){
        String b;
        if(courseMapper.checkDoc_url(courseEntity.getDoc_url())==null)
        {
            courseMapper.addDoc(courseEntity);
            b="文档添加成功!";
        }
        else b="该文档已存在！";
        return Mess.info(b);
    }
    //查询某个视频资源
    @CrossOrigin
    @RequestMapping(value="/all/video/{course_dir}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp VideoList(@PathVariable String course_dir){
        PageHelper.startPage(1, 10);
        if(course_dir==null||course_dir==""){
            return MyResp.render(0,"获取成功", courseMapper.findVideoAll(), courseMapper.findVideoCountAll());
        }else if(courseMapper.findDirByname(course_dir)==null){
            return MyResp.render(0,"该大类不存在!", courseMapper.findVideoAll(), courseMapper.findVideoCountAll());
        }else {
            return  MyResp.render(0,"获取成功", courseMapper.findAllVideoByDir(course_dir), courseMapper.findAllVideoCountByDir(course_dir));
        }
    }

    //查询某个文档资源
    @CrossOrigin
    @RequestMapping(value="/all/doc/{course_dir}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp DocList(@PathVariable String course_dir) {
        PageHelper.startPage(1, 10);
        if (course_dir == null || course_dir == "") {
            return MyResp.render(0, "获取成功", courseMapper.findDocAll(), courseMapper.findDocCountAll());
        } else if (courseMapper.findDirByname(course_dir) == null) {
            return MyResp.render(0, "该大类不存在!", courseMapper.findDocAll(), courseMapper.findDocCountAll());
        } else {
            return MyResp.render(0, "获取成功", courseMapper.findAllDocByDir(course_dir), courseMapper.findAllDocCountByDir(course_dir));
        }
    }

    //查询所有视频
    @CrossOrigin
    @RequestMapping(value="/all/video", method = RequestMethod.GET)
    @ResponseBody
    public MyResp AllVideoList(){
        PageHelper.startPage(1, 10);
        return MyResp.render(0,"获取成功", courseMapper.findVideoAll(), courseMapper.findVideoCountAll());
    }


    //查询所有文档
    @CrossOrigin
    @RequestMapping(value="/all/doc", method = RequestMethod.GET)
    @ResponseBody
    public MyResp AllDocList(){
        PageHelper.startPage(1, 10);
        return MyResp.render(0,"获取成功", courseMapper.findDocAll(), courseMapper.findDocCountAll());
    }

    //管理员查询学习者所选视频  测试OK
    @CrossOrigin
    @RequestMapping(value="/all/myvideo/{name}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp LearnerVideoList(HttpServletRequest request,@PathVariable String name){
        PageHelper.startPage(1, 10);
        String phone=learnerMapper.findLearnerPhoneByName(name);
        List<Course> list1=courseMapper.findMyVideoAll(phone);
        List<Course> list2=new ArrayList<Course>();
        for(int i=0;i<list1.size();i++){
            list2.add(list1.get(i));
            String course_name=courseMapper.findCourseName(list1.get(i).getCourse_id());
            list2.get(i).setCourse_name(course_name);
            int course_dir=courseMapper.findDir_class(list1.get(i).getCourse_id());
            list2.get(i).setCourse_dir(courseMapper.findDir(course_dir));
        }
        return MyResp.render(0,"获取成功", list2, courseMapper.findMyVideoCountAll(phone));
    }

    //管理员查询学习者所选文档  测试OK
    @CrossOrigin
    @RequestMapping(value="/all/mydoc/{name}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp LearnerDocList(HttpServletRequest request,@PathVariable String name){
        PageHelper.startPage(1, 10);
        String phone=learnerMapper.findLearnerPhoneByName(name);
        List<Course> list1=courseMapper.findMyDocAll(phone);
        List<Course> list2=new ArrayList<Course>();
        for(int i=0;i<list1.size();i++){
            list2.add(list1.get(i));
            String course_name=courseMapper.findCourseName(list1.get(i).getCourse_id());
            list2.get(i).setCourse_name(course_name);
            int course_dir=courseMapper.findDir_class(list1.get(i).getCourse_id());
            list2.get(i).setCourse_dir(courseMapper.findDir(course_dir));
        }
        return MyResp.render(0,"获取成功", list2, courseMapper.findMyDocCountAll(phone));
    }
    //管理员查看学习者个人信息  测试OK
    @CrossOrigin
    @RequestMapping(value = "/learner/info/{name}",method = RequestMethod.GET)
    @ResponseBody
    public Learner personalInfo(@PathVariable String name,HttpServletRequest request){
        String phone=learnerMapper.findLearnerPhoneByName(name);
        Learner learner = learnerMapper.findLearnerInfo(phone);
        if(learnerMapper.findCoursedir(learner.getPhone())==null)
        {
            learner.setCoursedir("未选择学习方向，请选择！");
        }else{
            learner.setCoursedir(learnerMapper.findCoursedir(learner.getPhone()));
        }
        return learner;
    }
    //管理员分配视频资源给学习者  测试OK
    @CrossOrigin
    @RequestMapping(value = "/tolearner/newvideo/{name}",method = RequestMethod.PUT)
    @ResponseBody
    public Mess AdminaddLearnerVideo(@RequestBody Course courseEntity,@PathVariable String name){
        String b;
        String phone=learnerMapper.findLearnerPhoneByName(name);
        int video_id=courseMapper.findVideoIdByName(courseEntity.getCourse_name());//video表视频ID
        int course_id=courseMapper.findCourseID(courseEntity.getCourse_name()); //course表课程ID
        if(courseMapper.findStudinglearner(phone,course_id)==null) {
            courseMapper.LearneraddVedio(phone,course_id,video_id);
            b="添加成功！";
        }else if(courseMapper.findVideoIdInStuding(phone,course_id)!=0){
            b="您已添加过该视频，无需重复添加！";
        }else{
            courseMapper.LearnerUpdateVedio(phone,course_id,video_id);
            b="添加成功！";
        }
        return Mess.info

                (b);
    }

    //管理员分配文档资源给学习者  测试OK
    @CrossOrigin
    @RequestMapping(value = "/tolearner/newdoc/{name}",method = RequestMethod.PUT)
    @ResponseBody
    public Mess AdminaddLearnerDoc(@RequestBody Course courseEntity,@PathVariable String name){
        String b;
        String phone=learnerMapper.findLearnerPhoneByName(name);
        int doc_id=courseMapper.findDocIdByName(courseEntity.getCourse_name());//video表视频ID
        int course_id=courseMapper.findCourseID(courseEntity.getCourse_name()); //course表课程ID
        if(courseMapper.findStudinglearner(phone,course_id)==null) {
            courseMapper.LearneraddDoc(phone,course_id,doc_id);
            b="添加成功！";
        }else if(courseMapper.findDocIdInStuding(phone,course_id)!=0){
            b="您已添加过该文档，无需重复添加！";
        }else{
            courseMapper.LearnerUpdateDoc(phone,course_id,doc_id);
            b="添加成功！";
        }

        return Mess.info

                (b);
    }

    //管理员添加学习方向  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/dir",method = RequestMethod.POST)
    @ResponseBody
    public Mess addDir(@RequestBody Course courseEntity){
        String b;
        if(courseMapper.findDirByname(courseEntity.getCourse_dir())==null)
        {
            System.out.println(courseEntity.getCourse_dir());
            courseMapper.addDir(courseEntity.getCourse_dir());
            b="大类添加成功!";
        }
        else b="该大类已存在！";
        return Mess.info

                (b);
    }
    //管理员修改学习方向  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/dir/{name}",method = RequestMethod.POST)
    @ResponseBody
    public Mess UpdateDir(@RequestBody Course courseEntity,@PathVariable String name){
        //name 为要修改大类的名字，course_dir是修改之后的名字
        String b;
        if(courseMapper.findDirByname(name)!=null)
        {
            courseMapper.updateDir(courseEntity.getCourse_dir(),name);
            b="大类修改成功!";
        }
        else b="不存在该大类！";
        return Mess.info(b);
    }
    //查询所有大类
    @CrossOrigin
    @RequestMapping(value="/all/dir", method = RequestMethod.GET)
    @ResponseBody
    public MyResp DirList(){
        //返回的name是大类的名字
        PageHelper.startPage(1, 10);
        List<Course> list=courseMapper.findAllDir();
        return  MyResp.render(0,"获取成功", list, courseMapper.findAllDirCount());
    }
    //删除学习大类  测试OK
    @CrossOrigin
    @RequestMapping(value = "/delete/dir/{name}", method = RequestMethod.DELETE)
    @ResponseBody
    public Mess DeleteDir(@PathVariable String name){
        String b;
        System.out.println(name);
        if(name!=null&&courseMapper.findDirByname(name)!=null){
            courseMapper.DeleteDir(name);
            b = "删除成功!";
        }else {
            b = "不存在该学习大类!";
        }
        return Mess.info(b);
    }
    //查询学习大类BYname  测试OK
    @CrossOrigin
    @RequestMapping(value = "/find/dir/{name}", method = RequestMethod.POST)
    @ResponseBody
    public Mess FindDirByname(@PathVariable String name){
        String b;
        if(name!=null&&courseMapper.findDirByname(name)!=null){
            b = "存在";
        }else {
            b = "不存在该学习大类!";
        }
        return Mess.info

                (b);
    }


    //学习者添加视频至我的视频  测试OK
    @CrossOrigin
    @RequestMapping(value = "/learner/newvideo",method = RequestMethod.PUT)
    @ResponseBody
    public Mess LearneraddVideo(@RequestBody Course courseEntity,HttpServletRequest request){
        String b;
        Learner learner =(Learner) request.getSession().getAttribute("user");
        int video_id=courseMapper.findVideoIdByName(courseEntity.getCourse_name());//video表视频ID
        int course_id=courseMapper.findCourseID(courseEntity.getCourse_name()); //course表课程ID
        if(courseMapper.findStudinglearner(learner.getPhone(),course_id)==null) {
            courseMapper.LearneraddVedio(learner.getPhone(),course_id,video_id);
            b="添加成功！";
        }else if(courseMapper.findVideoIdInStuding(learner.getPhone(),course_id)!=0){
            b="您已添加过该视频，无需重复添加！";
        }else{
            courseMapper.LearnerUpdateVedio(learner.getPhone(),course_id,video_id);
            b="添加成功！";
         }

        return Mess.info(b);
    }

    //学习者添加视频至我的文档  测试OK
    @CrossOrigin
    @RequestMapping(value = "/learner/newdoc",method = RequestMethod.PUT)
    @ResponseBody
    public Mess LearneraddDoc(@RequestBody Course courseEntity,HttpServletRequest request){
        String b;
        Learner learner =(Learner) request.getSession().getAttribute("user");
        int doc_id=courseMapper.findDocIdByName(courseEntity.getCourse_name());//video表视频ID
        int course_id=courseMapper.findCourseID(courseEntity.getCourse_name()); //course表课程ID
        if(courseMapper.findStudinglearner(learner.getPhone(),course_id)==null) {
            courseMapper.LearneraddDoc(learner.getPhone(),course_id,doc_id);
            b="添加成功！";
        }else if(courseMapper.findDocIdInStuding(learner.getPhone(),course_id)!=0){
            b="您已添加过该文档，无需重复添加！";
        }else{
            courseMapper.LearnerUpdateDoc(learner.getPhone(),course_id,doc_id);
            b="添加成功！";
        }

        return Mess.info(b);
    }
    //管理员修改视频资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/update/video",method = RequestMethod.POST)
    @ResponseBody
    public Mess UpdateVideo(@RequestBody Course courseEntity){
        String b;
        courseMapper.updateVideo(courseEntity);
        b="修改成功！";
        return Mess.info(b);
    }

    //管理员修改文档资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/update/doc",method = RequestMethod.POST)
    @ResponseBody
    public Mess UpdateDoc(@RequestBody Course courseEntity){
        String b;
        courseMapper.updateDoc(courseEntity);
        b="修改成功！";
        return Mess.info(b);
    }

    //学习者删除视频资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/delete/video/{course_name}", method = RequestMethod.DELETE)
    @ResponseBody
    public Mess deleteMyVideo(@PathVariable String course_name,HttpServletRequest request){
        String b;
        Learner learner =(Learner) request.getSession().getAttribute("user");
        if(course_name!=null){
            int video_id=courseMapper.findVideoIdByName(course_name);
            courseMapper.LearnerdeleteVideo(learner.getPhone(),video_id);
            b = "删除成功!";
        }else {
            b = "删除失败!";
        }
        return Mess.info(b);
    }

    //学习者删除文档资源  测试OK
    @CrossOrigin
    @RequestMapping(value = "/delete/doc/{course_name}", method = RequestMethod.DELETE)
    @ResponseBody
    public Mess deleteMyDoc(@PathVariable String course_name,HttpServletRequest request){
        String b;
        Learner learner =(Learner) request.getSession().getAttribute("user");
        if(course_name!=null){
            int video_id=courseMapper.findDocIdByName(course_name);
            courseMapper.LearnerdeleteDoc(learner.getPhone(),video_id);
            b = "删除成功!";
        }else {
            b = "删除失败!";
        }
        return Mess.info(b);
    }

    //学习者查询所选视频  兼搜索
    @CrossOrigin
    @RequestMapping(value="/all/myvideo", method = RequestMethod.GET)
    @ResponseBody
    public MyResp MyVideoList(HttpServletRequest request,
                            @RequestParam(value="value",required = false,defaultValue = "null") String value,
                            @RequestParam(value="type",required = false,defaultValue = "null") String type){
        Learner learner =(Learner) request.getSession().getAttribute("user");
        PageHelper.startPage(1, 10);
        switch (type){
            case "course_name":
                int course_id=courseMapper.findCourseID(value);
                List<Course> list1=courseMapper.findMyAllVideoByName(learner.getPhone(),course_id);
                List<Course> list2=new ArrayList<Course>();
                for(int i=0;i<list1.size();i++){
                    list2.add(list1.get(i));
                    String course_name=courseMapper.findCourseName(list1.get(i).getCourse_id());
                    list2.get(i).setCourse_name(course_name);
                    int course_dir=courseMapper.findDir_class(list1.get(i).getCourse_id());
                    list2.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                }
                return  MyResp.render(0,"获取成功", list2, courseMapper.findMyAllVideoCountByName(learner.getPhone(),course_id));
            case "":
                List<Course> list3=courseMapper.findMyVideoAll(learner.getPhone());
                List<Course> list4=new ArrayList<Course>();
                for(int i=0;i<list3.size();i++){
                    list4.add(list3.get(i));
                    String course_name=courseMapper.findCourseName(list3.get(i).getCourse_id());
                    list4.get(i).setCourse_name(course_name);
                    int course_dir=courseMapper.findDir_class(list3.get(i).getCourse_id());
                    list4.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                }
                return MyResp.render(0,"获取成功", list4, courseMapper.findMyVideoCountAll(learner.getPhone()));
            default:
                List<Course> list5=courseMapper.findMyVideoAll(learner.getPhone());
                List<Course> list6=new ArrayList<Course>();
                for(int i=0;i<list5.size();i++){
                    list6.add(list5.get(i));
                    String course_name=courseMapper.findCourseName(list5.get(i).getCourse_id());
                    int course_dir=courseMapper.findDir_class(list5.get(i).getCourse_id());
                    list6.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                    list6.get(i).setCourse_name(course_name);
                }
                return MyResp.render(0,"获取成功", list6, courseMapper.findMyVideoCountAll(learner.getPhone()));
        }
    }
    //查询所有课程(小类)BY大类名  测试OK
    @CrossOrigin
    @RequestMapping(value = "/all/course/{course_dir}", method = RequestMethod.GET)
    @ResponseBody
    public MyResp FindAllcourseList(@PathVariable String course_dir,HttpServletRequest request){
        String b;
        int course_dirID=courseMapper.findDirIDByname(course_dir);
        PageHelper.startPage(1, 10);
        List<Course> list=courseMapper.findAllCourse(course_dirID);
        return MyResp.render(0,"获取成功", list, courseMapper.findAlCourseCount(course_dirID));
    }

    //管理员添加某学习方向下的课程小类  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/course",method = RequestMethod.POST)
    @ResponseBody
    public Mess addCourse(@RequestBody Course courseEntity){
        String b;
        int course_dirID=courseMapper.findDirIDByname(courseEntity.getCourse_dir());
        if(courseMapper.findCourseByname(courseEntity.getName())==null)
        {
            courseMapper.addCourse(courseEntity.getName(),course_dirID);
            b="课程添加成功!";
        }
        else b="该课程已存在！";
        return Mess.info

                (b);
    }

    //查询小类BYname  测试OK
    @CrossOrigin
    @RequestMapping(value = "/find/course/{name}", method = RequestMethod.POST)
    @ResponseBody
    public Mess FindCourseByname(@PathVariable String name){
        String b;
        if(name!=null&&courseMapper.findCourseByname(name)!=null){
            b = "存在";
        }else {
            b = "不存在该课程!";
        }
        return Mess.info

                (b);
    }


    //删除学习小类  测试OK
    @CrossOrigin
    @RequestMapping(value = "/delete/course/{name}", method = RequestMethod.DELETE)
    @ResponseBody
    public Mess DeleteCourse(@PathVariable String name){
        String b;
        if(name!=null&&courseMapper.findCourseByname(name)!=null){
            courseMapper. DeleteCourse(name);
            b = "删除成功!";
        }else {
            b = "不存在该课程!";
        }
        return Mess.info(b);
    }

    //管理员修改学习小类  测试OK
    @CrossOrigin
    @RequestMapping(value = "/new/course/{name}",method = RequestMethod.POST)
    @ResponseBody
    public Mess UpdateCourse(@RequestBody Course courseEntity,@PathVariable String name){
        //name 为要修改小类的名字，course_dir是修改之后的所属大类名字,courseEntity.getName()是修改之后的小类名字
        String b;
        if(courseMapper.findDirByname(courseEntity.getCourse_dir())==null){
            b="大类不存在!";
        }else if(name!=null&&courseMapper.findCourseByname(name)!=null)
        {   int id =courseMapper.findDirIDByname(courseEntity.getCourse_dir());
            courseMapper.updateCourse(courseEntity.getName(),id,name);
            b="课程信息修改成功!";
        }
        else b="不存在该课程！";
        return Mess.info(b);
    }

    //学习者查询所选文档  兼搜索
    @CrossOrigin
    @RequestMapping(value="/all/mydoc", method = RequestMethod.GET)
    @ResponseBody
    public MyResp MyDocList(HttpServletRequest request,
                              @RequestParam(value="value",required = false,defaultValue = "null") String value,
                              @RequestParam(value="type",required = false,defaultValue = "null") String type){
        Learner learner =(Learner) request.getSession().getAttribute("user");
        PageHelper.startPage(1, 10);
        switch (type){
            case "course_name":
                int course_id=courseMapper.findCourseID(value);
                List<Course> list1=courseMapper.findMyAllDocByName(learner.getPhone(),course_id);
                List<Course> list2=new ArrayList<Course>();
                for(int i=0;i<list1.size();i++){
                    list2.add(list1.get(i));
                    String course_name=courseMapper.findCourseName(list1.get(i).getCourse_id());
                    list2.get(i).setCourse_name(course_name);
                    int course_dir=courseMapper.findDir_class(list1.get(i).getCourse_id());
                    list2.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                }
                return  MyResp.render(0,"获取成功", list2, courseMapper.findMyAllDocCountByName(learner.getPhone(),course_id));
            case "":
                List<Course> list3=courseMapper.findMyDocAll(learner.getPhone());
                List<Course> list4=new ArrayList<Course>();
                for(int i=0;i<list3.size();i++){
                    list4.add(list3.get(i));
                    String course_name=courseMapper.findCourseName(list3.get(i).getCourse_id());
                    list4.get(i).setCourse_name(course_name);
                    int course_dir=courseMapper.findDir_class(list3.get(i).getCourse_id());
                    list4.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                }
                return MyResp.render(0,"获取成功", list4, courseMapper.findMyDocCountAll(learner.getPhone()));
            default:
                List<Course> list5=courseMapper.findMyDocAll(learner.getPhone());
                List<Course> list6=new ArrayList<Course>();
                for(int i=0;i<list5.size();i++){
                    list6.add(list5.get(i));
                    String course_name=courseMapper.findCourseName(list5.get(i).getCourse_id());
                    list6.get(i).setCourse_name(course_name);
                    int course_dir=courseMapper.findDir_class(list5.get(i).getCourse_id());
                    list6.get(i).setCourse_dir(courseMapper.findDir(course_dir));
                }
                return MyResp.render(0,"获取成功", list6, courseMapper.findMyDocCountAll(learner.getPhone()));
        }
    }

    //获取当前时间
    private Date getNtime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

}
